package com.catsic.core.depart.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.catsic.base.BaseServiceImpl;
import com.catsic.base.exception.CatsicException;
import com.catsic.core.constants.Constants;
import com.catsic.core.convert.Converts;
import com.catsic.core.depart.dao.DepartDao;
import com.catsic.core.depart.service.DepartService;
import com.catsic.core.entity.Depart;
import com.catsic.core.entity.Organ;
import com.catsic.core.exception.CatsicExceptionCode;
import com.catsic.core.form.DepartForm;
import com.catsic.core.organ.service.OrganService;

/**
 * 
 * @author huyulei
 *
 */
@Service("departService")
public class DepartServiceImpl extends BaseServiceImpl<Depart,String> implements DepartService{

	@Autowired
	private OrganService organService;
	
	@Autowired
	private DepartDao departmentDao;
	
	/**
	 * @see com.catsic.core.base.BaseServiceImpl#add(java.lang.Object)
	 */
	@Override
	public DepartForm add(DepartForm form){
		Depart depart = new Depart();
		depart.setName(form.getName());
		/**
		 * 查询父节点
		 */
		Depart parent = null;
		if(form.getPid() != null){
			parent = findById(form.getPid());
		}
		depart.setParent(parent);
		
		if(StringUtils.isNotBlank(form.getOrganId())){
			Organ organ = organService.findById(form.getOrganId());
			depart.setOrgan(organ);
		}
		
		/**
		 * 查询父节点下的所有子节点
		 */
		List<Depart> childs = departmentDao.findByOrganId(form.getOrganId());
		if(CollectionUtils.isNotEmpty(childs)){
			String code = childs.get(childs.size()-1).getCode();
			int length = code.length();
			depart.setCode(String.format("%0" + length + "d", Long.valueOf(code)+ 1));
		}else {
			depart.setCode(parent == null ? depart.getOrgan().getCode() + String.format("%0" + Constants.编码位数 + "d",0) : depart.getOrgan().getCode() + String.format("%0" + Constants.编码位数 + "d",0));
		}
		depart = super.add(depart);
		return Converts.convert(depart);
	}
	
	/**
	 * @see com.catsic.core.depart.service.DepartService#update(com.catsic.core.form.DepartForm)
	 **/
	@Override
	public DepartForm update(DepartForm form){
		Depart depart = findById(form.getId());
		depart.setName(form.getName());
		depart = super.add(depart);
		return Converts.convert(depart);
	}
	
	/**
	 * @see com.catsic.core.base.BaseServiceImpl#delete(java.io.Serializable)
	 **/
	@Override
	public void delete(String id) {
		departmentDao.delete(id);
	}
	
	/**
	 * @see com.catsic.core.depart.service.DepartService#findDepartById(java.lang.String)
	 **/
	public DepartForm findDepartById(String id){
		DepartForm departForm = Converts.convert(findById(id));
		return departForm;
	}
	
	/**
	 * @see com.catsic.core.depart.service.DepartService#findById(java.lang.String)
	 **/
	@Override
	public Depart findById(String id) {
		Depart depart = super.getById(id);
		if(null == depart){
			throw new CatsicException(CatsicExceptionCode.实体不存在,Constants.DEPART);
		}
		return depart;
	}

	/**
	 * @see com.catsic.core.depart.service.DepartService#query(java.lang.String)
	 **/
	@Override
	public List<DepartForm> query(String organId) {
		List<Depart> list = findByOrganId(organId);
		List<DepartForm> listForm = new ArrayList<DepartForm>();
		for(Depart depart : list){
			DepartForm form = Converts.convert(depart);
			listForm.add(form);
		}
		return listForm;
	}

	/**
	 * @see com.catsic.core.depart.service.DepartService#findByOrganId(java.lang.String)
	 **/
	@Override
	public List<Depart> findByOrganId(String organId) {
		List<Depart> list = departmentDao.findByOrganId(organId);
		return list;
	}
}
